package com.tuanzi.loan.business.repository.wechat.impl;

import com.tuanzi.loan.business.repository.wechat.WechatCustomerMessageRepository;
import com.tuanzi.loan.business.repository.wechat.custom.WechatCustomerMessageCustomRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.vo.request.wechat.WechatCustomerMessageRequest;
import com.tuanzi.loan.vo.response.wechat.WechatCustomerMessageResponse;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import java.time.format.DateTimeFormatter;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanWechatCustomerMessage.LOAN_WECHAT_CUSTOMER_MESSAGE;

/**
 * @Author: chenweijun
 * @Date: 2018/9/27 17:52
 * @Description:
 */
@Repository
@SuppressWarnings({ "rawtypes" })
public class WechatCustomerMessageRepositoryImpl extends JOOQBaseRepository implements WechatCustomerMessageCustomRepository{


    @Override
    protected TableImpl getTable() {
        return LOAN_WECHAT_CUSTOMER_MESSAGE;
    }

    @Override
    public Page<WechatCustomerMessageResponse> list(WechatCustomerMessageRequest searchRequest, Pageable pageable) {

        fields(LOAN_WECHAT_CUSTOMER_MESSAGE.fields());
        contains(LOAN_WECHAT_CUSTOMER_MESSAGE.NAME, searchRequest.getName());
        eq(LOAN_WECHAT_CUSTOMER_MESSAGE.MSG_TYPE, searchRequest.getMsgType());
        eq(LOAN_WECHAT_CUSTOMER_MESSAGE.STATUS, searchRequest.getStatus());
        eq(LOAN_WECHAT_CUSTOMER_MESSAGE.PRODUCT_ID, searchRequest.getProductId());
        eq(LOAN_WECHAT_CUSTOMER_MESSAGE.ID, searchRequest.getId());

        orderBy(LOAN_WECHAT_CUSTOMER_MESSAGE.UPDATE_TIME, Sort.Direction.DESC);


        return search(pageable, record -> {

            WechatCustomerMessageResponse response = new WechatCustomerMessageResponse();
            response.setId(record.get(LOAN_WECHAT_CUSTOMER_MESSAGE.ID));
            response.setName(record.get(LOAN_WECHAT_CUSTOMER_MESSAGE.NAME));
            response.setMsgType(record.get(LOAN_WECHAT_CUSTOMER_MESSAGE.MSG_TYPE));
            response.setEffectiveStartTime(record.get(LOAN_WECHAT_CUSTOMER_MESSAGE.EFFECTIVE_START_TIME));
            response.setEffectiveEndTime(record.get(LOAN_WECHAT_CUSTOMER_MESSAGE.EFFECTIVE_END_TIME));
            response.setProductId(record.get(LOAN_WECHAT_CUSTOMER_MESSAGE.PRODUCT_ID));
            response.setStatus(record.get(LOAN_WECHAT_CUSTOMER_MESSAGE.STATUS).intValue());
            response.setUpdator(record.get(LOAN_WECHAT_CUSTOMER_MESSAGE.UPDATOR));
            response.setUpdateTime(record.get(LOAN_WECHAT_CUSTOMER_MESSAGE.UPDATE_TIME));

            return response;
        });
    }

}
